package cn.itsource.quartz.service.impl;

import cn.itsource.quartz.domain.QuartzInfo;
import cn.itsource.quartz.service.IQuartzService;
import cn.itsource.quartz.utils.QuartzUtils;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Service;

@Service
public class QuartzServiceImpl implements IQuartzService {

    @Autowired
    private SchedulerFactoryBean factoryBean;

    //添加定时任务
    /*
     * @param sched  调度器
     * @param jobName 任务名
     * @param cls   任务
     * @param params   任务参数
     * @param time  时间设置，参考quartz说明文档
     */
    @Override
    public void addJob(QuartzInfo quartzInfo) {
        try {
            Scheduler scheduler = factoryBean.getScheduler();
            //添加定时任务
            QuartzUtils.addJob(scheduler, quartzInfo.getJobName(),quartzInfo.getCla()
                    ,quartzInfo.getParams() ,quartzInfo.getCorn() );
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*
     * @param sched 调度器
     * @param jobName
     */
    //移出定时任务
    @Override
    public void removeJob(String jobName) {
        try {
            Scheduler scheduler = factoryBean.getScheduler();
            QuartzUtils.removeJob(scheduler, jobName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
